/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.UserManagement;
import jard.webshop.nbp.Address;
import jard.webshop.nbp.User;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Harteex
 */
@ManagedBean(name = "registerBacking")
@RequestScoped
public class RegisterBacking {

    User user;

    /** Creates a new instance of RegisterBacking */
    public RegisterBacking() {
        user = new User();
        user.setAddress(new Address());
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void verifyPasswordValidator(FacesContext facesContext,
            UIComponent uiComponent,
            Object newValue) {
        String repeatPass = (String) newValue;
        String pass = (String) uiComponent.getAttributes().get("password");
        
        // Debug
        System.out.println("(RegisterBacking.verifyPasswordValidator) pass=" + pass + ", repeatPass=" + repeatPass);
        
        if (repeatPass == null || !repeatPass.equals(pass)) {
            throw new ValidatorException(new FacesMessage("Does not match supplied password!"));
        }
    }
    
    public void checkEmailInDatabase(FacesContext facesContext,
            UIComponent uiComponent,
            Object newValue) {
        
        System.out.println("(RegisterBacking.checkEmailInDatabase) checking email address in database");
        
        if (UserManagement.getInstance().getUser((String) newValue) != null)
            throw new ValidatorException(new FacesMessage("Email address already in use!"));
    }
}
